import {
    Component
} from 'react';

import {
    Datetime,
    Field,
    PhotoField,
    Switch,
    Group,
    EmployeeField,
    Boxs,
    TextField,
    TextareaField,
    SelectField,
    DatetimeField,
    Avatar,
    Toast,
} from 'saltui';

const {
    HBox,
    Box,
    VBox
} = Boxs;
import './Field.less';
import Loading from '../loading';

let uploadAttaching = false;

export default class AttachmentField extends Component {

    constructor(props) {
        super(props);
        this.state = {

        }
    }

    handleDel = function(e) {
        // var delIndex = e.target.innerHTML;
        // alert(JSON.stringify(e));
        var delIndex = e.target.getAttribute('data-key');
        var findIndex = 0;
        this.props.attachmentList.map((item, index) => {
            if (item.fileId == delIndex) {
                findIndex = index;
            }
        });
        if (findIndex == -1) {
            findIndex = 0;
        }

        var list = [
            ...this.props.attachmentList.slice(0, findIndex),
            ...this.props.attachmentList.slice(findIndex + 1)
        ];
        // alert(JSON.stringify(list));
        this.props.onDel(list);

    }

    calculate = function(no) {
        let a = no / 1024;
        if (a < 1) {
            return no + 'B';
        }
        let b = a / 1024;
        if (b < 1) {
            return Math.round(a * 100) / 100 + 'KB';
        }
        return Math.round(b * 100) / 100 + 'MB';
    }

    handleAdd2 = function(e) {
        if (uploadAttaching == false) {
            Toast.show({
                content: <Loading/>,
                autoHide: false
            });
            let tt = this;
            if (this.props.attachmentList.length >= 9) {
                Toast.show({
                    type: 'error',
                    content: '最多只可以上传9个附件！',
                });
                return;
            }
            uploadAttaching = true;
            //let e1= e;
            allowupload(function(result) {
                uploadAttaching = false;
                Toast.hide(); //关闭提示
                var result = JSON.parse(result);
                // alert(JSON.stringify(result));
                var spaceid = result.spaceid;
                dd.biz.util.uploadAttachment({
                    image: {
                        multiple: true,
                        compress: false,
                        max: 9,
                        spaceId: spaceid + ''
                    },
                    space: {
                        corpId: corpid + '',
                        spaceId: spaceid + '',
                        isCopy: 1,
                        max: 9
                    },
                    file: {
                        spaceId: spaceid + '',
                        max: 9
                    },
                    types: ["file", "space"],
                    onSuccess: function(result) {
                        console.log(JSON.stringify(result));
                        var attMent = result.data;

                        e.preventDefault();
                        if (tt.props.attachmentList.length + attMent.length > 9) {
                            Toast.show({
                                type: 'error',
                                content: '最多只可以上传9个附件！',
                            });
                            return;
                        }
                        var rows = tt.props.attachmentList;
                        for (var i = 0; i < attMent.length; i++) {
                            // attchName = attMent[i].fileName;
                            // var object={};
                            // object["filename"]=attchName;
                            attMent[i].fileDisplayName = attMent[i].fileName;

                            // attchmentFile.push(attchName);
                            // tt.props.attachment.push(attMent[i]);
                            // var rows = tt.props.todol;
                            // rows.push(attchName);
                            // tt.props.onAdd(attMent[i]);
                            rows.push(attMent[i]);
                        }
                        // alert("rows: "+ JSON.stringify(rows));
                        tt.props.onAdd(rows);

                    },
                    onFail: function(err) {}
                });

            });
        }

    }

    render() {
        const t = this;
        let lists;
        if (this.props.attachmentList && this.props.attachmentList instanceof Array && this.props.attachmentList.length > 0) {
            lists = this.props.attachmentList.map(function(item, i) {
                return (
                    <HBox hAlign="start" vAlign="start" className="dd-t-border t-P6 t-MB10" data-key={item.fileId}
                         onClick={t.handleDel.bind(t)}>
                        <i className="iconfont icon-file t-FCc t-FL t-MR6"></i>
                        <VBox hAlign="start" vAlign="start" className="dd-t-list-text-content">
                            <span className="t-list-title t-LH1_4">
                                {item.fileDisplayName}
                            </span>
                            <span className="dd-t-list-time t-FBAC">{t.calculate(item.fileSize)}</span>
                        </VBox>
                    </HBox>
                );
            });
        }

        return (
            <div className="t-group-list ">
                <div className="t-field-box t-FBH t-FBAC t-FBJ">
                    <div className="t-field-layout-h-label t-FS16">附件添加</div>
                    <div flex={1} onClick={this.handleAdd2.bind(this)} name="新增项" ref="addnew"
                         className="t-PR dd-file-add">
                        <i className="iconfont icon-plus"></i>
                    </div>
                </div>

                <HBox className="t-field-box  dd-upload-file">
                    {lists}
                </HBox>
            </div>
        );
    }
}